Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
error-polyfill
Advanced tools
Implementing the V8 Stack Trace API in non-V8 environments as much as possible
npm install error-polyfill
bower install error-polyfill
Tested on the following environments:
Windows 7
Travis
The polyfill might work on other environments too due to its adaptive design. I use Karma with Browserify to test the framework in browsers.
ES5 support is required, without that the lib throws an Error and stops working.
The ES5 features are tested by the capability lib run time. Classes are created by the o3 lib. Utility functions are implemented in the u3 lib.
In this documentation I used the framework as follows:
require("error-polyfill");
// <- your code here
It is recommended to require the polyfill in your main script.
Error.getStackTrace
This static method is not part of the V8 Stack Trace API, but it is recommended to use Error.getStackTrace(throwable)
instead of throwable.stack
to get the stack trace of Error instances!
Explanation:
By non-V8 environments we cannot replace the default stack generation algorithm, so we need a workaround to generate the stack when somebody tries to access it. So the original stack string will be parsed and the result will be properly formatted by accessing the stack using the Error.getStackTrace
method.
Arguments and return values:
throwable
argument should be an Error
(descendant) instance, but it can be an Object
instance as well.stack
of the throwable
argument.Example:
try {
theNotDefinedFunction();
}
catch (error) {
console.log(Error.getStackTrace(error));
// ReferenceError: theNotDefinedFunction is not defined
// at ...
// ...
}
Error.captureStackTrace
The Error.captureStackTrace(throwable [, terminator])
sets the present stack above the terminator
on the throwable
.
Arguments and return values:
throwable
argument should be an instance of an Error
descendant, but it can be an Object
instance as well. It is recommended to use Error
descendant instances instead of inline objects, because we can recognize them by type e.g. error instanceof UserError
.terminator
argument should be a Function
. Only the calls before this function will be reported in the stack, so without a terminator
argument, the last call in the stack will be the call of the Error.captureStackTrace
.stack
will be set on the throwable
so you will be able to access it using Error.getStackTrace
. The format of the stack depends on the Error.prepareStackTrace
implementation.Example:
var UserError = function (message){
this.name = "UserError";
this.message = message;
Error.captureStackTrace(this, this.constructor);
};
UserError.prototype = Object.create(Error.prototype);
function codeSmells(){
throw new UserError("What's going on?!");
}
codeSmells();
// UserError: What's going on?!
// at codeSmells (myModule.js:23:1)
// ...
Limitations:
By the current implementation the terminator
can be only the Error.captureStackTrace
caller function. This will change soon, but in certain conditions, e.g. by using strict mode ("use strict";
) it is not possible to access the information necessary to implement this feature. You will get an empty frames
array and a warning
in the Error.prepareStackTrace
when the stack parser meets with such conditions.
Error.prepareStackTrace
The Error.prepareStackTrace(throwable, frames [, warnings])
formats the stack frames
and returns the stack
value for Error.captureStackTrace
or Error.getStackTrace
. The native implementation returns a stack string, but you can override that by setting a new function value.
Arguments and return values:
throwable
argument is an Error
or Object
instance coming from the Error.captureStackTrace
or from the creation of a new Error
instance. Be aware that in some environments you need to throw that instance to get a parsable stack. Without that you will get only a warning
by trying to access the stack with Error.getStackTrace
.frames
argument is an array of Frame
instances. Each frame
represents a function call in the stack. You can use these frames to build a stack string. To access information about individual frames you can use the following methods.frame.toString()
- Returns the string representation of the frame, e.g. codeSmells (myModule.js:23:1)
.frame.getThis()
- Cannot be supported. Returns the context of the call, only V8 environments support this natively.frame.getTypeName()
- Not implemented yet. Returns the type name of the context, by the global namespace it is Window
in Chrome.frame.getFunction()
- Returns the called function or undefined
by strict mode.frame.getFunctionName()
- Not implemented yet. Returns the name of the called function.frame.getMethodName()
- Not implemented yet. Returns the method name of the called function is a method of an object.frame.getFileName()
- Not implemented yet. Returns the file name where the function was called.frame.getLineNumber()
- Not implemented yet. Returns at which line the function was called in the file.frame.getColumnNumber()
- Not implemented yet. Returns at which column the function was called in the file. This information is not always available.frame.getEvalOrigin()
- Not implemented yet. Returns the original of an eval
call.frame.isTopLevel()
- Not implemented yet. Returns whether the function was called from the top level.frame.isEval()
- Not implemented yet. Returns whether the called function was eval
.frame.isNative()
- Not implemented yet. Returns whether the called function was native.frame.isConstructor()
- Not implemented yet. Returns whether the called function was a constructor.warnings
argument contains warning messages coming from the stack parser. It is not part of the V8 Stack Trace API.Error.getStackTrace(throwable)
. If it is an object, it is recommended to add a toString
method, so you will be able to read it in the console.Example:
Error.prepareStackTrace = function (throwable, frames, warnings) {
var string = "";
string += throwable.name || "Error";
string += ": " + (throwable.message || "");
if (warnings instanceof Array)
for (var warningIndex in warnings) {
var warning = warnings[warningIndex];
string += "\n # " + warning;
}
for (var frameIndex in frames) {
var frame = frames[frameIndex];
string += "\n at " + frame.toString();
}
return string;
};
Error.stackTraceLimit
Not implemented yet.
You can set size limits on the stack trace, so you won't have any problems because of too long stack traces.
Example:
Error.stackTraceLimit = 10;
Not implemented yet.
Since there is no Stack Trace API standard, every browsers solves this problem differently. I try to document what I've found about these differences as detailed as possible, so it will be easier to follow the code.
Overriding the error.stack
property with custom Stack instances
Error.prepareStackTrace()
can override every error.stack
automatically right by creationerror.stack
by native errorserror.stack
property of native errors, it is not configurableCapturing the current stack trace
Error.prepareStackTrace()
is called only by the first accessError.captureStackTrace()
Accessing the stack
error.stack
propertyerror.stacktrace
property to get the stackPrefixes and postfixes on the stack string
error.name
and the error.message
in a {name}: {message}
format at the beginning of the stack stringerror.name
and the error.message
Accessing the stack frames array
Error.prepareStackTrace()
The structure of the frame string
thirdFn (http://localhost/myModule.js:45:29)
at
prefix, which is not present by the frame.toString()
output, so it is added by the stack.toString()
thirdFn@http://localhost/myModule.js:45:29
at thirdFn (http://localhost/myModule.js:45:29)
thirdFn@http://localhost/myModule.js:45:29
at thirdFn (http://localhost/myModule.js:45)
Accessing information by individual frames
frame.getThis()
and the frame.getFunction()
returns undefined
by frames originate from strict mode codeframe.getThis()
cannot be implementedarguments.callee.caller
by frames originate from strict mode, so by these frames frame.getFunction()
can return only undefined
(this is consistent with V8 behavior)MIT - 2016 Jánszky László Lajos
FAQs
Javascript Error Polyfill
The npm package error-polyfill receives a total of 77,715 weekly downloads. As such, error-polyfill popularity was classified as popular.
We found that error-polyfill demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.